Git安装使用

1.安装git

        centos上

1
[root@localhost ~]# yum install -y epel-release
1
[root@localhost ~]# yum install -y git

        ubuntu上

1
sudo apt-get install git

        windows 上安装 msysgit,下载地址

        安装完成后,还需要最后一步设置

1
2
[root@localhost ~]# git config --global user.name "yanyi"
[root@localhost ~]# git config --global user.email "hcldir@qq.com"

        设置完成会生成 .gitconfig 文件

1
2
3
4
5
6
[root@localhost ~]# ls -la
-rw-r--r-- 1 root root 44 1月 6 20:01 .gitconfig
[root@localhost ~]# cat .gitconfig
[user]
name = yanyi
email = hcldir@qq.com

2.创建版本库

1
2
3
4
[root@localhost ~]# mkdir /home/gitroot
[root@localhost ~]# cd /home/gitroot
[root@localhost gitroot]# git init
Initialized empty Git repository in /home/gitroot/.git/

        说明:git init 用这个命令初始化,让这个目录变成git可以管理的仓库。

        ls -a 可以看到多了一个 .git 文件

1
2
[root@localhost gitroot]# ls -a
. .. .git

3.提交文件到仓库

        创建一个文件 1.txt

1
2
3
4
5
6
[root@localhost gitroot]# echo -e "123\naaa\n456\nbbb" > 1.txt
[root@localhost gitroot]# cat 1.txt
123
aaa
456
bbb

        把1.txt添加到仓库中

1
[root@localhost gitroot]# git add 1.txt

        add 完了必须要 commit 才算真正把文件提交到git仓库里

1
2
3
4
[root@localhost gitroot]# git commit -m "add new file 1.txt"
[master (root-commit) 0214d82] add new file 1.txt
1 files changed, 4 insertions(+), 0 deletions(-)
create mode 100644 1.txt

        更改 1.txt后,查看和版本库是否一致

1
2
3
4
5
6
7
8
9
[root@localhost gitroot]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: 1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

        说明:git status 查看当前仓库中的状态,比如是否有改动的文件

        如果不提交,需要把文件恢复成版本库里的文件

1
2
3
4
5
6
[root@localhost gitroot]# git checkout -- 1.txt
[root@localhost gitroot]# cat 1.txt
123
aaa
456
bbb

        再次执行 git status 命令,提示没有任何更改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost gitroot]# git status
# On branch master
nothing to commit (working directory clean)
```
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次修改 1.txt文件,对比修改内容
```bash
[root@localhost gitroot]# echo -e "111111\n2222222222" >> 1.txt
[root@localhost gitroot]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: 1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index b149eee..aa2d2ae 100644
--- a/1.txt
+++ b/1.txt
@@ -2,3 +2,5 @@
aaa
456
bbb
+111111
+2222222222

说明:git diff 1.txt 可以对比1.txt本次修改了什么内容,相比较仓库里面的版本。

        把更改个文件提交到版本库

1
2
3
4
5
6
7
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m "add a line 1.txt"
[master 182c98f] add a line 1.txt
1 files changed, 2 insertions(+), 0 deletions(-)
[root@localhost gitroot]# git status
# On branch master
nothing to commit (working directory clean)

4.版本回退

        多更改几次1.txt,并进行 git add, git commit操作

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost gitroot]# echo "asdfkjsadkjf" >> 1.txt
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m "change 1.txt agin"
[master 0c72aba] change 1.txt agin
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost gitroot]# echo "123456" >> 1.txt
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m "change 1.txt agin agin"
[master e4fe595] change 1.txt agin agin
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost gitroot]# git status
# On branch master
nothing to commit (working directory clean)

        git log 查看所有提交git仓库的记录操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost gitroot]# git log
commit e4fe59596fc6c16757cb1d772d775932701f86ab
Author: yanyi <hcldir@qq.com>
Date: Fri Jan 6 21:57:43 2017 +0800
change 1.txt agin agin
commit 0c72aba21aba27ccc9c9873ec53c552d5474a02f
Author: yanyi <hcldir@qq.com>
Date: Fri Jan 6 21:56:41 2017 +0800
change 1.txt agin
commit 182c98ff8798f47965ba44ed218e9da5e9964295
Author: yanyi <hcldir@qq.com>
Date: Fri Jan 6 20:34:15 2017 +0800
add a line 1.txt
commit 0214d8252fd3c37a7f09fc32e8ff29d10d577230
Author: yanyi <hcldir@qq.com>
Date: Fri Jan 6 20:15:19 2017 +0800
add new file 1.txt

        根据这个 log 可退回到前边某个版本

        git log –pretty=oneline 命令可以让每个版本单独一行显示,更清晰。

1
2
3
4
5
[root@localhost gitroot]# git log --pretty=oneline
e4fe59596fc6c16757cb1d772d775932701f86ab change 1.txt agin agin
0c72aba21aba27ccc9c9873ec53c552d5474a02f change 1.txt agin
182c98ff8798f47965ba44ed218e9da5e9964295 add a line 1.txt
0214d8252fd3c37a7f09fc32e8ff29d10d577230 add new file 1.txt

        git reset –hard 字符串 可以退回到以前版本,字符串可以简写,只写前边几位

1
2
[root@localhost gitroot]# git reset --hard 182c
HEAD is now at 182c98f add a line 1.txt

        退回版本后,在 git log 则无法显示退回版本以上的版本

1
2
3
[root@localhost gitroot]# git log --pretty=oneline
182c98ff8798f47965ba44ed218e9da5e9964295 add a line 1.txt
0214d8252fd3c37a7f09fc32e8ff29d10d577230 add new file 1.txt

        git reflog 可以显示所有版本

1
2
3
4
5
6
7
[root@localhost gitroot]# git reflog
e4fe595 HEAD@{0}: e4fe5: updating HEAD
182c98f HEAD@{1}: 182c: updating HEAD
e4fe595 HEAD@{2}: commit: change 1.txt agin agin
0c72aba HEAD@{3}: commit: change 1.txt agin
182c98f HEAD@{4}: commit: add a line 1.txt
0214d82 HEAD@{5}: commit (initial): add new file 1.txt

        然后可以使用 git reset –hard 字符串 再次退回版本。

5.撤销修改(文件恢复)

        如果修改文件后,发现改的不对,想恢复到上一次提交的状态,或不小心删除了文件。可以使用

        git checkout – file 恢复到上一次提交的状态。

1
[root@localhost gitroot]# git checkout 1.txt

        如果文件修改完成,保存后,git add 了,但没有 commit ,想退回到上一次提交的状态可以用git reset HEAD ,再使用 git checkout – file

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: 1.txt
#
[root@localhost gitroot]# git reset HEAD 1.txt
Unstaged changes after reset:
M 1.txt
[root@localhost gitroot]# git checkout -- 1.txt

        如果不仅 add 又 commit 了,就用版本退回恢复到上一次提交状态。

6.文件删除

        新建 2.txt 文件,add 并 commit 提交到 git 仓库。

1
2
3
4
5
6
[root@localhost gitroot]# echo "2222222" >> 2.txt
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m " a new 2.txt"
[master 44ec005] a new 2.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 2.txt

        下面删除它

1
[root@localhost gitroot]# rm -rf 2.txt

        git status 可以看到,提示 2.txt 被删除

1
2
3
4
5
6
7
8
9
[root@localhost gitroot]# git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: 2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

        如果需要恢复就用 git checkout – 2.txt,现在是要从 git 仓库里删除 2.txt,命令 git rm file

1
2
3
4
5
6
[root@localhost gitroot]# git rm 2.txt
rm '2.txt'
[root@localhost gitroot]# git commit -m "delete 2.txt"
[master b01be1a] delete 2.txt
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 2.txt

        这样就彻底从 git 仓库删除 2.txt 了

7.创建远程仓库(github)

        首先到 https://github.com/ 注册一个账号,创建自己的git,点repositories 再点new

        名字自定义,比如叫studygit 选择public 点 create repository

        添加key:

        右上角点自己头像,选择settings,左侧选择SSH and GPG keys

        填写好标题,key 在自己主机上创建,命令 ssh-keygen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost gitroot]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
1f:5f:50:04:2b:80:16:16:e1:9a:a8:59:8c:0a:83:f4 root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| ==. .oo |
| oo . o |
| . .. . o |
|oo.. o . . |
|= +Eo S . . |
|o= . o . |
|+ . . |
| |
| |
+-----------------+

        右上角点New SSH key,把linux机器上的 /root/.ssh/id_rsa.pub内容粘贴到这里

1
2
[root@localhost gitroot]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAmTutzYNSxeZNJmHZXtczs8UbfvPZAkKF/Kwj4f8rlD5IG0Mml+euYjgzb2ppQzvh/sxqFJOKBvM9MhQ5MRc2t4eLm1U11FLyxLLJh6Kdtc6do8lWRJLJFOomkky6DQ4ISN8RinW6GMaT22H41xQ4LozIoA2EbYKELsno6MxbFqfwJlwSgv6WB3oBwDWYaY6nf7c04K2jH/xJwxLD8OyO+SzI15JdQBuZrgAXIvh5sQwwX6spKBKqyeYhTOGXZkkI8FSiY90ZMJrsxwZvbZVMNrpvFnXFN5f8ZHeVM33pTpT05EMLt+FrHixuccP8EJjq4efcwQQcrZJoFJYJZSy5vQ== root@localhost

        把本地仓库推送到远程仓库,命令

1
2
git remote add origin git@github.com:hcldirgit/studygit.git
git push -u origin master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost home]# mkdir studygit
[root@localhost home]# cd studygit
[root@localhost studygit]# echo "# studygit" >> README.md
[root@localhost studygit]# git init
itialized empty Git repository in /home/studygit/.git/
[root@localhost studygit]# git add README.md
[root@localhost studygit]# git commit -m "first commit"
[master (root-commit) f3cba66] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README.md
[root@localhost studygit]# git remote add origin git@github.com:hcldirgit/studygit.git
[root@localhost studygit]# git push -u origin master
The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 3.12 KiB, done.
Total 6 (delta 0), reused 0 (delta 0)
To git@github.com:hcldirgit/studygit.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

        下一次再推送,就可以直接 git push

8.克隆远程仓库

        在 github 上想把别人的仓库克隆下来

1
2
3
4
5
6
7
8
9
10
[root@localhost studygit]# cd /home/
[root@localhost home]# git clone git@github.com:aminglinux/lanmp.git
Initialized empty Git repository in /home/lanmp/.git/
Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.
remote: Counting objects: 26, done.
remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 26
Receiving objects: 100% (26/26), 5.46 KiB, done.
Resolving deltas: 100% (4/4), done.
[root@localhost home]# ls
gitroot lanmp studygit

        它提示,会在当前目录下初始化一个仓库,并创建一个.git的目录

1
Initialized empty Git repository in /home/lanmp/.git/

        完成后可以看到一个lanmp 的目录

1
2
3
4
5
6
7
8
9
10
[root@localhost home]# ls
gitroot lanmp studygit
[root@localhost home]# cd lanmp
[root@localhost lanmp]# ls
lanmp.sh README.md
[root@localhost lanmp]# cat README.md
# lanmp
lamp/lnmp 一键安装脚本
author: aming
version: 0.2

        编辑修改后,使用 git push 再推送到远程服务端。

        注:如果是克隆的别人的 git 仓库,则无法推送,因为别人没有加我的 key 。

9.使用分支

        查看分支,命令 git branch

1
2
[root@localhost studygit]# git branch
* master

        创建分支,命令 git branch 分支名

1
[root@localhost studygit]# git branch yanyi

        切换到创建分之下,git checkout 分支名

1
2
[root@localhost studygit]# git checkout yanyi
Switched to branch 'yanyi'

        在新分支下,创建文件并提交

1
2
3
4
5
6
[root@localhost studygit]# echo "234qw34\qwerqwer" > 2.txt
[root@localhost studygit]# git add 2.txt
[root@localhost studygit]# git commit -m "add new 2.txt"
[yanyi acbc9ea] add new 2.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 2.txt

        切换到 master 分支,查看

1
2
3
4
[root@localhost studygit]# git checkout master
Switched to branch 'master'
[root@localhost studygit]# ls
lanmp.sh README.md

10.分支的合并和删除

        把 yanyi 分支合并到 master ,命令 git merge yanyi 。注意必须先切换到 master 分支下执行命令

1
2
3
4
5
6
7
8
9
10
[root@localhost studygit]# git checkout master
Switched to branch 'master'
[root@localhost studygit]# git merge yanyi
Updating 23545a4..acbc9ea
Fast-forward
2.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 2.txt
[root@localhost studygit]# ls
2.txt lanmp.sh README.md

        如果master分支和yanyi分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。

        解决冲突的方法是在master分支下,编辑2.txt,改为yanyi分支里面2.txt的内容。 然后提交2.txt,再合并yanyi分支。

        但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 我们可以编辑2.txt内容,改为我们想要的,然后提交。切换到yanyi分支,然后合并master分支到yanyi分支即可。(倒着合并)合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。

        删除分支,命令 git branch -d 分支名

        如果分支没有合并,删除之前会提示。不合并,强制删除,命令 git branch -D 分支名

11.分支使用原则

        对于分支的应用,建议大家以这样的原则来:

        master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。

        创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master

        开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支

12.git stash 保留现场

        当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作。问题是,你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决这个问题的办法就是git stash命令。

        比如我们在 yanyi 分支,编辑了一个新的文件3.txt

        这时候我们需要到其他分支去修复一个bug,所以需要先git add 3.txt

        然后 git stash 保存一下现场

        再切换到另外分支去修复bug,修复完bug后,再回到 yanyi 分支

        git stash list 可以看到我们保存过的现场

        用 git stash apply 恢复现场

        也可以指定stash:

        git stash apply stash@{0}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost studygit]# echo "wqerqwer\asdasg\q1234234" > 3.txt
[root@localhost studygit]# ls
2.txt 3.txt lanmp.sh README.md
[root@localhost studygit]# git add 3.txt
[root@localhost studygit]# git stash
Saved working directory and index state WIP on yanyi: acbc9ea add new 2.txt
HEAD is now at acbc9ea add new 2.txt
[root@localhost studygit]# git stash list
stash@{0}: WIP on yanyi: acbc9ea add new 2.txt
[root@localhost studygit]# git stash apply stash@{0}
# On branch yanyi
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: 3.txt
#
[root@localhost studygit]# ls
2.txt 3.txt lanmp.sh README.md

13.远程分支管理

        查看远程库信息,使用git remote -v 本地新建的分支如果不推送到远程,对其他人就是不可见的

1
2
3
[root@localhost studygit]# git remote -v
origin git@github.com:hcldirgit/studygit.git (fetch)
origin git@github.com:hcldirgit/studygit.git (push)

        查看远程分支 git ls-remote origin

1
2
3
4
5
[root@localhost studygit]# git ls-remote origin
acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 HEAD
acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/dev
acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/master
acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 refs/heads/yanyi

        从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交

1
2
[root@localhost studygit]# git push origin yanyi
Everything up-to-date
1
2
[root@localhost studygit]# git pull
Already up-to-date.

        在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost home]# rm -rf studygit
[root@localhost home]# git clone git@github.com:hcldirgit/studygit.git
Initialized empty Git repository in /home/studygit/.git/
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 9 (delta 0), reused 9 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.
[root@localhost home]# cd studygit
[root@localhost studygit]# ls
2.txt lanmp.sh README.md
[root@localhost studygit]# git branch
* master
[root@localhost studygit]# git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[root@localhost studygit]# git checkout -b yanyi origin/yanyi
Branch yanyi set up to track remote branch yanyi from origin.
Switched to a new branch 'yanyi'
[root@localhost studygit]# git checkout -b yi origin/yi
Branch yi set up to track remote branch yi from origin.
Switched to a new branch 'yi'
[root@localhost studygit]# git branch
dev
master
yanyi
* yi

        从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

14.标签管理

        标签类似于快照功能,我们可以给版本库打一个标签,记录某个时刻库的状态。我们可以随时恢复到该状态。

git checkout master 先切到master分支上

git tag v1.0 给master打一个标签v1.0

git tag 可以查看所有的标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost studygit]# git tag v1.0
[root@localhost studygit]# git tag
v1.0
[root@localhost studygit]# git show v1.0
commit acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1
Author: yanyi <hcldir@qq.com>
Date: Mon Jan 9 05:08:10 2017 +0800
add new 2.txt
diff --git a/2.txt b/2.txt
new file mode 100644
index 0000000..e9af856
--- /dev/null
+++ b/2.txt
@@ -0,0 +1 @@
+234qw34\qwerqwer

        tag是针对commit来打标签的,所以可以针对历史的commit来打tag

        命令 git log –pretty=oneline –abbrev-commit

1
2
3
4
5
6
7
8
[root@localhost studygit]# git log --pretty=oneline
acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1 add new 2.txt
23545a469bcea58ba25718515d1de2c78c9d7235 add lanmp.sh
f3cba6670b29b8848c8a8ea61777918f097bcfbe first commit
[root@localhost studygit]# git log --pretty=oneline --abbrev-commit
acbc9ea add new 2.txt
23545a4 add lanmp.sh
f3cba66 first commit

        git tag v0.9 23545a4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost studygit]# git tag v0.9 23545a4
[root@localhost studygit]# git tag
v0.9
v1.0
[root@localhost studygit]# git show v0.9
commit 23545a469bcea58ba25718515d1de2c78c9d7235
Author: yanyi <hcldir@qq.com>
Date: Sun Jan 8 04:13:11 2017 +0800
add lanmp.sh
diff --git a/lanmp.sh b/lanmp.sh
new file mode 100644
index 0000000..5a1db53
--- /dev/null
+++ b/lanmp.sh
@@ -0,0 +1,465 @@

        git tag -a v0.8 -m “tag just v1.1 and so on” acbc9ea 可以对标签进行描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost studygit]# git tag -a v0.8 -m "tag just v1.1 and so on" acbc9ea
[root@localhost studygit]# git tag
v0.8
v0.9
v1.0
[root@localhost studygit]# git show v0.8
tag v0.8
Tagger: yanyi <hcldir@qq.com>
Date: Mon Jan 9 19:32:17 2017 +0800
tag just v1.1 and so on
commit acbc9ea125b527e9f3ab30dcf5f5b3e415c760e1
Author: yanyi <hcldir@qq.com>
Date: Mon Jan 9 05:08:10 2017 +0800
add new 2.txt
diff --git a/2.txt b/2.txt
new file mode 100644
index 0000000..e9af856
--- /dev/null
+++ b/2.txt
@@ -0,0 +1 @@
+234qw34\qwerqwer

        git tag -d v0.8 删除标签

1
2
[root@localhost studygit]# git tag -d v0.8
Deleted tag 'v0.8' (was 2321146)

        git push origin v1.0 推送指定标签到远程

1
2
3
4
[root@localhost studygit]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:hcldirgit/studygit.git
* [new tag] v1.0 -> v1.0

        git push –tag origin 推送所有标签

1
2
3
4
[root@localhost studygit]# git push --tag origin
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:hcldirgit/studygit.git
* [new tag] v0.9 -> v0.9

        如果本地删除了一个标签,远程也想要删除需要这样操作:

        git tag v1.0 -d

        git push origin :refs/tags/v1.0

1
2
3
4
5
[root@localhost studygit]# git tag v1.0 -d
Deleted tag 'v1.0' (was acbc9ea)
[root@localhost studygit]# git push origin :refs/tags/v1.0
To git@github.com:hcldirgit/studygit.git
- [deleted] v1.0

15.git 别名

        git commit 这个命令是不是有点长? 用别名可以提高我们的工作效率

        git config –global alias.ci commit

        git config –global alias.co checkout

        git config –global alias.br branch

1
2
3
4
5
6
7
8
9
10
11
[root@localhost studygit]# git config --global alias.ci comit
[root@localhost studygit]# git config --global alias.br branch
[root@localhost studygit]# git config --global alias.co checkout
[root@localhost studygit]# git config --global alias.lg "log --pretty=oneline"
[root@localhost studygit]# git br
dev
* master
yanyi
yi
[root@localhost studygit]# git co dev
Switched to branch 'dev'

        别名所在文件 /root/.gitconfig 也可以直接编辑该文件

1
2
3
4
5
6
7
8
9
[root@localhost studygit]# cat /root/.gitconfig
[user]
name = yanyi
email = hcldir@qq.com
[alias]
ci = comit
br = branch
co = checkout
lg = log --pretty=oneline

        查看git别名使用命令

        git config –list |grep alias

1
2
3
4
5
[root@localhost studygit]# git config --list |grep alias
alias.ci=comit
alias.br=branch
alias.co=checkout
alias.lg=log --pretty=oneline

        git config –list 可以详细查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost studygit]# git config --list
user.name=yanyi
user.email=hcldir@qq.com
alias.ci=comit
alias.br=branch
alias.co=checkout
alias.lg=log --pretty=oneline
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=git@github.com:hcldirgit/studygit.git
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.dev.remote=origin
branch.dev.merge=refs/heads/dev
branch.yanyi.remote=origin
branch.yanyi.merge=refs/heads/yanyi
branch.yi.remote=origin
branch.yi.merge=refs/heads/yi

        查询log小技巧

1
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
1
2
3
4
5
[root@localhost studygit]# git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
[root@localhost studygit]# git lg
* acbc9ea - (HEAD, origin/yi, origin/yanyi, origin/master, origin/dev, origin/HEAD, yi, yanyi, master, dev) add new 2.txt (15 hours ago) <yanyi>
* 23545a4 - (v0.9) add lanmp.sh (2 days ago) <yanyi>
* f3cba66 - first commit (2 days ago) <yanyi>

        使用以后,git 就会有颜色

        取消别名

        git config –global –unset alias.br

1
2
3
4
5
6
7
[root@localhost studygit]# git config --global --unset alias.br
[root@localhost studygit]# git br
git: 'br' is not a git command. See 'git --help'.
Did you mean this?
var
`

16.搭建 git 服务器

        github 毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。

        yum install git安装git

1
2
[root@gitserver ~]# yum install -y epel-release
[root@gitserver ~]# yum install -y git

        useradd -s /usr/bin/git-shell git 添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆

1
2
[root@gitserver ~]# useradd -s /usr/bin/git-shell git
[root@gitserver ~]# cd /home/git

        创建 authorized_keys 文件,并更改属主、属组和权限,用来存客户端机器上的公钥

1
2
3
[root@gitserver git]# mkdir .ssh
[root@gitserver git]# touch .ssh/authorized_keys
[root@gitserver git]# vim .ssh/authorized_keys

        公钥内容在,客户端 /root/.shh/id_rsa.pub

1
[root@localhost studygit]# cat /root/.ssh/id_rsa.pub

        设置权限

1
2
3
[root@gitserver git]# chown -R git .ssh
[root@gitserver git]# chmod 700 .ssh
[root@gitserver git]# chmod 600 .ssh/authorized_keys

        客户端查看能否连接

1
2
3
4
5
6
7
[root@localhost studygit]# ssh git@192.168.0.91
The authenticity of host '192.168.0.91 (192.168.0.91)' can't be established.
RSA key fingerprint is 38:82:75:f9:11:a3:56:b4:00:ce:46:75:65:7c:5c:9b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.91' (RSA) to the list of known hosts.
fatal: What do you think I am? A shell?
Connection to 192.168.0.91 closed.

        这样就说明可以连接

        主机设置

1
[root@gitserver git]# usermod -s /bin/bash git

        客户端连接

1
2
3
4
5
[root@localhost studygit]# ssh git@192.168.0.91
Last login: Mon Jan 9 21:53:51 2017 from 192.168.0.92
[git@gitserver ~]$ logout
Connection to 192.168.0.91 closed.
[root@localhost studygit]#

        这样检测客户端公钥能登录主机,检测完成主机改回设置

1
[root@gitserver git]# usermod -s /usr/bin/git-shell git

        这样客户端就不能登录主机

        定好存储git仓库的目录,比如 /data/gitroot

1
2
3
4
5
6
7
8
9
[root@gitserver git]# mkdir -p /data/gitroot
[root@gitserver git]# cd /data/gitroot
[root@gitserver gitroot]# git init --bare sample.git
Initialized empty Git repository in /data/gitroot/sample.git/
[root@gitserver gitroot]# ls -la
总用量 12
drwxr-xr-x 3 root root 4096 1月 9 22:16 .
drwxr-xr-x 3 root root 4096 1月 9 22:16 ..
drwxr-xr-x 7 root root 4096 1月 9 22:16 sample.git

        说明:git init –bare sample.git 会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾

        更改权限

1
[root@gitserver gitroot]# chown -R git.git sample.git

        在客户端上(自己pc)克隆远程仓库

1
2
3
4
5
[root@localhost home]# git clone git@192.168.0.91:/data/gitroot/sample.git
Initialized empty Git repository in /home/sample/.git/
warning: You appear to have cloned an empty repository.
[root@localhost home]# ls
gitroot lanmp sample studygit

        客户端操作,推送到服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost sample]# ls
[root@localhost sample]# echo "1234\abcd\5678\efgh" > 1.txt
[root@localhost sample]# git add 1.txt
[root@localhost sample]# git commit -m "add new 1.txt"
[master (root-commit) a86f429] add new 1.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 1.txt
[root@localhost sample]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 224 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.0.91:/data/gitroot/sample.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

        以上操作是在git服务器上做的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的。

        首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里 git clone git@ip:/data/gitroot/sample.git

        此时就可以在当前目录下生成一个sample的目录,这个就是我们克隆的远程仓库了。进入到这里面,可以开发一些代码,然后push到远程。